﻿/**
  NELMATE   ELMATEC   LM      EL  TECONELMA  CO         NE          MA   ONELMAT    ELMATECO  LMATECONE 
 ON        NE     CO  LMA     EL  TE         CO         NEL        LMA  CO      EC     AT     LM       
CO         NE     CO  LMAT    EL  TE         CO         NELM      ELMA  CO      EC     AT     LM
CO         NE     CO  LM TE   EL  TECONELM   CO         NE MA    NE MA  CONELMATEC     AT     LMATECONE
CO         KC     CO  LM  EC  EL  TE         CO         NE  AT  ON  MA  CO      EC     AT     LM
 ON        KC     CO  LM   CO EL  TE         CO         NE   TECO   MA  CO      EC     AT     LM
  NELMATE   ELMATEC   LM    ONEL  TECONELMA  CONELMATE  NE    EC    MA  CO      EC     AT     LMATECONE
_______________________________________________________________________________________________________
 ConelMate Framework 3.0 - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	Connect
	@vertion: 	1.0
	@date:		25/02/08
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/


package com.conelmate.nc
{
	
	import com.conelmate.util.Trace;
	
	import flash.net.NetConnection;
	import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;	
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.net.ObjectEncoding;


	public class NetConn extends Trace
	{
		static	public	const 	CONNECT_SUCCESS		:String = "CONNECT_SUCCESS";		
		static	public	const 	CONNECT_CLOSED		:String = "CONNECT_CLOSED";
		static	public	const 	CONNECT_FAILED		:String = "CONNECT_FAILED";
		static	public	const 	CONNECT_REJECTED	:String = "CONNECT_REJECTED";
		static	public	const 	CONNECT_APPSHUTDOWN	:String = "CONNECT_APPSHUTDOWN";
		static	public	const 	CONNECT_INVALIDAPP	:String = "CONNECT_INVALIDAPP";		
		static	public	const 	CALL_FAILED			:String = "CALL_FAILED";
		static	public	const 	CALL_PROHIBITED		:String = "CALL_PROHIBITED";		
		static	public	const	SECURITY_ERROR		:String = "SECURITY_ERROR";
		
				private var 	_NC					:NetConnection;			//coneccion				
				private var 	_STR_CONNECTION		:String = "";			//string de coneccion
				private var 	_RTMP				:String = "";			//ip
				private var 	_INSTANCE			:String = "";			//instancia
				private var 	_PORT				:String = "";			//puerto
				private var 	_ROOM				:String = "";			//sala
				private var 	_ARGUMENTS									//datos a pasar como parametros luego de connect
				private var		_strError			:String = "";
				private var		_strMsg				:String = "";


//_________________________________________________________________________________ Constructor	
		public function NetConn(rtmp:String = "", port:String = "", instance:String = "", room:String = "")
		{
			_isTrace = false;
			mTrace("#NetConn# constructor");
			
			_RTMP 		= _RTMP		|| rtmp;
			_PORT 		= _PORT 	|| port;
			_INSTANCE	= _INSTANCE	|| instance;
			_ROOM 		= _ROOM		|| room;
			
			_NC = new NetConnection();
			_NC.objectEncoding = flash.net.ObjectEncoding.AMF0;
			_NC.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            _NC.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		}
		


//_________________________________________________________________________________ Public Methods

		/**
		 * inicializar
		 */		
		public function connect():Boolean
		{
			mTrace("#NetConn# connect");
			
			if(_RTMP && _PORT && _INSTANCE){
			
				if(_ROOM != "") _STR_CONNECTION = "rtmp://" + _RTMP + ":" + _PORT + "/" + _INSTANCE + "/" + _ROOM;
				else			_STR_CONNECTION = "rtmp://" + _RTMP + ":" + _PORT + "/" + _INSTANCE;
				
				_NC.connect(_STR_CONNECTION, _ARGUMENTS);
				return true;
			} else return false;
		}
		
		
		/**
		 * destruir objetos y variables
		 */	
		public function destroy():void
		{
			mTrace("#NetConn# destroy");
			
			if(_NC){
				_NC.close();
				_NC = null;
			}
		}
		
	/*	public function close(e):void
		{
		}
	*/	
//_________________________________________________________________________________ Private Methods	

		/**
		 * al recibir una respuesta del servidor
		 */		
        private function netStatusHandler(e:NetStatusEvent):void 
		{
			
		//	trace("netStatusHandler: "+e.info)
		//	for(var i in e.info.application) trace("netStatusHandler: "+i+" "+e.info.application[i]);
			if(e.info){
				if(e.info.application){
					
					trace("error: "+e.info.application.error)
					trace("msg: "+e.info.application.msg)
					
					if(e.info.application.error) _strError = e.info.application.error;
					else 						 _strError = "";
					if(e.info.application.msg) 	 _strMsg = e.info.application.msg;
					else 						 _strMsg = "";
				}
			}
			
			var info:String = e.info.code;
			mTrace("#NetConn# Status: " + info);			
			
				 if(info == "NetConnection.Connect.Success") 	dispatchEvent(new Event(NetConn.CONNECT_SUCCESS));				 
			else if(info == "NetConnection.Connect.Closed") 	dispatchEvent(new Event(NetConn.CONNECT_CLOSED));
			else if(info == "NetConnection.Connect.Failed") 	dispatchEvent(new Event(NetConn.CONNECT_FAILED));
			else if(info == "NetConnection.Connect.Rejected") 	dispatchEvent(new Event(NetConn.CONNECT_REJECTED));
			else if(info == "NetConnection.Connect.AppShutdown")dispatchEvent(new Event(NetConn.CONNECT_APPSHUTDOWN));
			else if(info == "NetConnection.Connect.InvalidApp") dispatchEvent(new Event(NetConn.CONNECT_INVALIDAPP));			
			else if(info == "NetConnection.Call.Failed") 		dispatchEvent(new Event(NetConn.CALL_FAILED));
			else if(info == "NetConnection.Call.Prohibited") 	dispatchEvent(new Event(NetConn.CALL_PROHIBITED));
        }
		

		/**
		 * al ocurrir un error
		 */		
        private function securityErrorHandler(e:SecurityErrorEvent):void 
		{
            mTrace("#NetConn# securityErrorHandler: " + e);
			dispatchEvent(new Event(NetConn.SECURITY_ERROR));
        }



//_________________________________________________________________________________ GETTERS
		public function get nc				():NetConnection{ return _NC; }
		public function get rtmp			():String 		{ return _RTMP; }
		public function get port			():String 		{ return _PORT; }
		public function get instance		():String 		{ return _INSTANCE; }
		public function get room			():String 		{ return _ROOM; }
		public function get strConnection	():String		{ return _STR_CONNECTION; }
		public function get error			():String		{ return _strError; }
		public function get msg				():String		{ return _strMsg; }

//_________________________________________________________________________________ SETTERS
		public function set rtmp		(p:String)	{ _RTMP = p; }
		public function set port		(p:String)	{ _PORT = p; }
		public function set instance	(p:String)	{ _INSTANCE = p; }
		public function set room		(p:String)	{ _ROOM = p; }
		public function set arguments	(p)			{ _ARGUMENTS = p; }

	}
}